<!--
Tests that free-floating anchors can be created in an AR session.
-->
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="../resources/webxr_e2e.css">
  </head>
  <body>
    <canvas id="webgl-canvas"></canvas>
    <script src="../../../../../../third_party/blink/web_tests/resources/testharness.js"></script>
    <script src="../resources/webxr_e2e.js"></script>
    <script>var shouldAutoCreateNonImmersiveSession = false;</script>
    <script src="../resources/webxr_boilerplate.js"></script>
    <script>
      setup({single_test: true});

      const TestState = Object.freeze({
        "Initial":  0,
        "SpaceObtained": 1,
        "AnchorRequested": 2,
        });

      let testState = TestState.Initial;

      function stepStartTest() {
        const sessionInfo = sessionInfos[sessionTypes.AR];
        const referenceSpace = sessionInfo.currentRefSpace;
        let localReferenceSpace = null;

        testState = TestState.Initial;

        sessionInfo.session.requestReferenceSpace("local").then(localSpace => {
          localReferenceSpace = localSpace;
          testState = TestState.SpaceObtained;
        });

        onARFrameCallback = (session, frame) => {
          switch(testState) {
            case TestState.SpaceObtained: {
              // We should attempt to create an anchor once the tracking is established.
              // We can observe whether the tracking was established by inspecting the relationship between
              // viewer space and local space - if the pose is non-null and non-emulated, the AR is in
              // steady state.
              const viewerPose = frame.getViewerPose(localReferenceSpace);
              if(viewerPose && !viewerPose.emulatedPosition) {
                frame.createAnchor(new XRRigidTransform(), referenceSpace).then((anchor) => {
                  done();
                  return;
                }).catch((err) => {
                  // Fail the test.
                  assert_unreached("XRFrame.createAnchor() promise rejected.");
                });

                testState = TestState.AnchorRequested;
              }
              return;
            }
            default:
              return;
          }
        };
      }
    </script>
  </body>
</html>
